package cheme.Interpreter.AST.Interfaces;

import java.util.ArrayList;
import java.util.List;

public class SubjectCore implements ISubject, Cloneable
{
	List<IObserver> observers = null;
	ISubject host = null;
	
	public SubjectCore(ISubject host)
	{
		this.host = host;
	}
	
	@Override
	public Object clone() throws CloneNotSupportedException
	{
		SubjectCore clone  = new SubjectCore(host);
		
		if(null != observers)
		{
			for(IObserver o : observers)
			{
				clone.attach(o);
			}
		}
		
		return super.clone();
	}
	
	public void attach(IObserver o)
	{
		if(null == observers)
		{
			observers = new ArrayList<IObserver>();
		}
		
		if(!observers.contains(o))
		{
			observers.add(o);
		}
	}

	public void detach(IObserver o)
	{
		if(null != observers)
		{
			observers.remove(o);
		}
	}
	
	public void notifyObservers()
	{
		if(null != observers)
		{
			for(IObserver o : observers)
			{
				o.notify(host);
			}
		}
	}
}